package org.dev.hi.hiui.search

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.TypedValue
import org.dev.hi.hiui.R
import org.dev.hi.library.util.HiDisPlayUtils
import org.dev.hi.library.util.HiRes

/**
 * Author WenPing
 * CreateTime 2021/8/23.
 * Description:
 */
object AttrsParse {

    fun parseAttrs(
        context: Context,
        attributeSet: AttributeSet? = null,
        defstyle: Int = 0
    ): Attrs {
        // TypedValue 动态存储属性数据
        val value = TypedValue()
        // 解析app的style文件中,appTheme中是否有当前hiSearchViewStyle 节点
        context.theme.resolveAttribute(R.attr.hiSearchViewStyle, value, true)
        // 如果当前主题下的节点中有hiSearchViewStyle,否则就用hiui中默认的
        val defStyleRes = if (value.resourceId != 0) value.resourceId else R.style.searchViewStyle

        /**
         * attributeSet 构造函数中
         * R.styleable.HiSearchView serachview的属性集合
         * defstyle 构造函数中
         * defStyleRes 资源值
         */
        val array = context.obtainStyledAttributes(
            attributeSet,
            R.styleable.HiSearchView,
            defstyle,
            defStyleRes
        )

        // 解析数据
        //搜索背景
        val searchBackground = array.getDrawable(R.styleable.HiSearchView_search_background)
            ?: HiRes.getDrawable(R.drawable.shape_search_view)
        //搜索图标
        val searchIcon = array.getString(R.styleable.HiSearchView_search_icon)
        val searchIconSize = array.getDimensionPixelSize(
            R.styleable.HiSearchView_search_icon_size,
            HiDisPlayUtils.sp2px(16f)
        )
        val iconPadding = array.getDimensionPixelOffset(
            R.styleable.HiSearchView_icon_padding,
            HiDisPlayUtils.sp2px(4f)
        )

        //清除按钮
        val clearIcon = array.getString(R.styleable.HiSearchView_clear_icon)
        val clearIconSize = array.getDimensionPixelSize(
            R.styleable.HiSearchView_clear_icon_size,
            HiDisPlayUtils.sp2px(16f)
        )

        //提示语
        val hintText = array.getString(R.styleable.HiSearchView_hint_text)
        val hintTextSize = array.getDimensionPixelSize(
            R.styleable.HiSearchView_hint_text_size,
            HiDisPlayUtils.sp2px(16f)
        )
        val hintTextColor = array.getColor(
            R.styleable.HiSearchView_hint_text_color,
            HiRes.getColor(R.color.hi_tabtop_normal_color)
        )
        //相对位置
        val gravity = array.getInteger(R.styleable.HiSearchView_hint_gravity, 1)

        //输入文本
        val searchTextSize = array.getDimensionPixelSize(
            R.styleable.HiSearchView_search_text_size,
            HiDisPlayUtils.sp2px(16f)
        )
        val searchTextColor = array.getColor(
            R.styleable.HiSearchView_search_text_color,
            HiRes.getColor(R.color.hi_tabtop_normal_color)
        )

        //keyword关键词
        val keywordSize = array.getDimensionPixelSize(
            R.styleable.HiSearchView_key_word_size,
            HiDisPlayUtils.sp2px(13f)
        )
        val keywordColor = array.getColor(R.styleable.HiSearchView_key_word_color, Color.WHITE)
        val keywordMaxLen = array.getInteger(R.styleable.HiSearchView_key_word_max_length, 10)
        val keywordBackground = array.getDrawable(R.styleable.HiSearchView_key_word_background)

        //关键词清除图标
        val keywordClearIcon = array.getString(R.styleable.HiSearchView_clear_icon)
        val keywordClearIconSize = array.getDimensionPixelSize(
            R.styleable.HiSearchView_clear_icon_size,
            HiDisPlayUtils.sp2px(12f)
        )


        array.recycle()

        return Attrs(
            searchBackground,
            searchIcon,
            searchIconSize.toFloat(),
            iconPadding,
            clearIcon,
            clearIconSize.toFloat(),
            hintText,
            hintTextSize.toFloat(),
            hintTextColor,
            gravity,
            searchTextSize.toFloat(),
            searchTextColor,
            keywordSize.toFloat(),
            keywordColor,
            keywordMaxLen,
            keywordBackground,
            keywordClearIcon,
            keywordClearIconSize.toFloat()
        )


    }

    data class Attrs(
        /*search view background*/
        val searchBackground: Drawable?,
        /*search icon 🔍*/
        val searchIcon: String?,
        val searchIconSize: Float,
        val iconPadding: Int,
        /*clearIcon*/
        val clearIcon: String?,
        val clearIconSize: Float,
        /*hint*/
        val hintText: String?,
        val hintTextSize: Float,
        val hintTextColor: Int,
        val gravity: Int,
        /*search text*/
        val searchTextSize: Float,
        val searchTextColor: Int,
        /*keyword*/
        val keywordSize: Float,
        val keywordColor: Int,
        val keywordMaxLen: Int,
        val keywordBackground: Drawable?,
        val keywordClearIcon: String?,
        val keywordClearIconSize: Float
    )
}